﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;
using Excel = Microsoft.Office.Interop.Excel;
using System.Collections;
using System.Data.SqlClient;
using GeneralLibs;


namespace ListForms
{
    public partial class FrmEditHangHoa_Thuoc : ListForms.FrmBaseEditForm
    {
        public FrmEditHangHoa_Thuoc(Lists.VatTuHangHoa VatTuHangHoa)
        {
            try
            {
                InitializeComponent();
                _DoiTuong = VatTuHangHoa;

                BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBanLe, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBanBuon, BasicForms.TextFormatType.Money);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        public FrmEditHangHoa_Thuoc(string ten)
        {
            try
            {
                InitializeComponent();
                _DoiTuong = null;
                txtTen.EditValue = ten;

                BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBanLe, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBanBuon, BasicForms.TextFormatType.Money);

                cmbNhomVatTuHangHoa.Focus();
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        public FrmEditHangHoa_Thuoc(string ma, string ten)
        {
            try
            {
                InitializeComponent();
                _DoiTuong = null;
                txtMa.EditValue = ma;
                txtTen.EditValue = ten;

                BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBanLe, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBanBuon, BasicForms.TextFormatType.Money);

                cmbNhomVatTuHangHoa.Focus();
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        Store _Store = DanhMucStore.Instance.GetByStoreID(Config.StoreID);

        protected override string GetMa()
        {
            return this.txtMa.Text.Trim();
        }
        protected override void GenNewBarCode(bool isNull)
        {
            try
            {
                if (isNull)
                {
                    txtMa.EditValue = DanhMucVatTuHangHoa.GenBarCode();
                }
                else
                {
                    txtMa.EditValue = null;
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        protected override void FillData()
        {
            cmbNhomVatTuHangHoa.DataSource = DanhMucNhomVatTuHangHoa.Filter("LienQuanDenKho = 1").DataView;
            cmbNhomVatTuHangHoa.DisplayColumn = "Ten";
            cmbNhomVatTuHangHoa.ValueColumn = "ID";
            cmbNhomVatTuHangHoa.AddVisibleColumn("Ma", "Mã");
            cmbNhomVatTuHangHoa.AddVisibleColumn("Ten", "Tên");

            if (_DoiTuong == null)
            {
                try
                {
                    cmbNhomVatTuHangHoa.Focus();
                    this.Text = "Thêm mới thuốc";
                    _DoiTuong = DanhMucVatTuHangHoa.Instance.NewDoiTuong();
                    txtGiaBia.EditValue = (decimal)0;
                    txtChietKhauBanLe.EditValue = (int)0;
                    txtChietKhauBan.EditValue = (int)0;
                    txtChietKhauMua.EditValue = (int)0;

                    txtGiaBanBuon.EditValue = (decimal)0;

                    cmbNhaSanXuat.DataSource = DanhMucVatTuHangHoa.GetAllNhaSanXuat();
                    cmbNhaSanXuat.DisplayMember = "NhaSanXuat";
                    cmbNhaSanXuat.Text = "";

                    cmbNguonCungCap.DataSource = DanhMucVatTuHangHoa.GetAllNguonCungCap();
                    cmbNguonCungCap.DisplayMember = "NguonCungCap";
                    cmbNguonCungCap.Text = "";

                    cmbDangBaoChe.DataSource = DanhMucVatTuHangHoa.GetAllTacGia_DangBaoChe();
                    cmbDangBaoChe.DisplayMember = "TacGia";
                    cmbDangBaoChe.Text = "";

                    cmbDonViTinh.DataSource = DanhMucVatTuHangHoa.GetAllDonViTinh();
                    cmbDonViTinh.DisplayMember = "DonViTinh";
                    cmbDonViTinh.Text = "";

                    _EditState = EditState.CreatingNew;
                }
                catch (SqlException sqlEx)
                {
                    throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
                }
                catch (Exception ex)
                {
                    if (ex is CustomException) throw ex;
                    else
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
            else
            {
                try
                {
                    cmbNhomVatTuHangHoa.Focus();

                    #region Doc du lieu

                    this.Text = "Sửa thông tin thuốc";
                    try
                    {
                        txtTenNganGon.Text = (_DoiTuong as Lists.VatTuHangHoa).TenNganGon;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        cmbNhaSanXuat.Text = (_DoiTuong as Lists.VatTuHangHoa).NhaSanXuat;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        cmbNguonCungCap.Text = (_DoiTuong as Lists.VatTuHangHoa).NguonCungCap;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtChietKhauBanLe.EditValue = (_DoiTuong as Lists.VatTuHangHoa).ChietKhauBanLe;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtChietKhauBan.EditValue = (_DoiTuong as Lists.VatTuHangHoa).ChietKhauBan;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtChietKhauMua.EditValue = (_DoiTuong as Lists.VatTuHangHoa).ChietKhauMua;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        rdoSuDung.Checked = (_DoiTuong as Lists.VatTuHangHoa).TrangThai;
                        rdoKhongSuDung.Checked = !(rdoSuDung.Checked);
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        cmbNhomVatTuHangHoa.EditValue = (_DoiTuong as Lists.VatTuHangHoa).NhomVatTuHangHoa.ID;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtMa.Text = (_DoiTuong as Lists.VatTuHangHoa).Ma;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtGiaBia.EditValue = (_DoiTuong as Lists.VatTuHangHoa).GiaBia;

                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtGhiChu.EditValue = (_DoiTuong as Lists.VatTuHangHoa).GhiChu;
                    }
                    catch
                    { }
                    try
                    {
                        nmrHeSoInMaVach.Text = (_DoiTuong as Lists.VatTuHangHoa).HeSoInMaVach.ToString();
                    }
                    catch
                    { }
                    try
                    {
                        txtHoatChat.EditValue = (_DoiTuong as Lists.VatTuHangHoa).HoatChat;
                    }
                    catch
                    { }
                    try
                    {
                        txtHamLuong.EditValue = (_DoiTuong as Lists.VatTuHangHoa).HamLuong;
                    }
                    catch
                    { }
                    try
                    {
                        txtDuongDung.EditValue = (_DoiTuong as Lists.VatTuHangHoa).DuongDung;
                    }
                    catch
                    { }
                    try
                    {
                        chkKhongHienKhiKiemtra.Checked = (_DoiTuong as Lists.VatTuHangHoa).KhongHienKhiKiemTra;
                    }
                    catch
                    { }

                    //For NTThanhBinh
                    try
                    {
                        txtGiaBanBuon.EditValue = (_DoiTuong as Lists.VatTuHangHoa).GiaBanHienTai;
                    }
                    catch
                    { }
                    try
                    {
                        cmbDangBaoChe.DataSource = DanhMucVatTuHangHoa.GetAllTacGia_DangBaoChe();
                        cmbDangBaoChe.DisplayMember = "TacGia";
                        cmbDangBaoChe.Text = (_DoiTuong as Lists.VatTuHangHoa).TacGia;
                    }
                    catch
                    { }

                    //tên đặt cuối cùng để cho các control khác valueChanged hết đi --> giá trị chuẩn
                    try
                    {
                        txtTen.Text = (_DoiTuong as Lists.VatTuHangHoa).Ten;
                    }
                    catch (Exception ex)
                    { }
                    #endregion

                    cmbNhaSanXuat.DataSource = DanhMucVatTuHangHoa.GetAllNhaSanXuat();
                    cmbNhaSanXuat.DisplayMember = "NhaSanXuat";
                    cmbNhaSanXuat.Text = (_DoiTuong as Lists.VatTuHangHoa).NhaSanXuat;

                    cmbNguonCungCap.DataSource = DanhMucVatTuHangHoa.GetAllNguonCungCap();
                    cmbNguonCungCap.DisplayMember = "NguonCungCap";
                    cmbNguonCungCap.Text = (_DoiTuong as Lists.VatTuHangHoa).NguonCungCap;

                    cmbDonViTinh.DataSource = DanhMucVatTuHangHoa.GetAllDonViTinh();
                    cmbDonViTinh.DisplayMember = "DonViTinh";
                    cmbDonViTinh.Text = (_DoiTuong as Lists.VatTuHangHoa).DonViTinh;

                    chkForOTC.Checked = (_DoiTuong as Lists.VatTuHangHoa).ForOTC;

                    _EditState = EditState.Modifying;

                    txtMa.Properties.ReadOnly = Lists.VatTuHangHoa.DaDuocGiaoDich(_DoiTuong.ID) &&
                        (Config.DuocPhepSuaMaHangKhiDaGiaoDich == DuocPhepSuaMaHangKhiDaGiaoDichHayKhong.KhongDuocPhep);
                }
                catch (Exception ex)
                {
                }
            }
        }
        protected override bool CheckFullInfo()
        {
            if (txtMa.Text == "") throw new CustomException("Chưa điền mã thuốc", ExceptionType.LackOfInputInfo);
            if (txtTen.Text == "") throw new CustomException("Chưa điền tên thuốc", ExceptionType.LackOfInputInfo);
            if (cmbNhomVatTuHangHoa.EditValue == null) throw new CustomException("Chưa chọn nhóm thuốc", ExceptionType.LackOfInputInfo);
            if (txtGiaBia.EditValue == null && txtGiaBia.Enabled) throw new CustomException("Chưa nhập giá bán lẻ mặc định", ExceptionType.LackOfInputInfo);
            if (rdoSuDung.Checked == false && rdoKhongSuDung.Checked == false) throw new CustomException("Chưa chọn thuốc sử dụng hay không sử dụng", ExceptionType.LackOfInputInfo);
            if (_EditState == EditState.Modifying && Lists.VatTuHangHoa.DaDuocGiaoDich(_DoiTuong.ID)
                && txtMa.Text != (_DoiTuong as Lists.VatTuHangHoa).Ma && Config.DuocPhepSuaMaHangKhiDaGiaoDich != DuocPhepSuaMaHangKhiDaGiaoDichHayKhong.DuocPhep)
            {
                throw new CustomException("Không được sửa mã của thuốc đã được giao dịch");
            }
            return true;
        }

        protected override void SetDoiTuongProperties()
        {
            try
            {
                string maHang = txtMa.Text.Trim();
                string tenHang = txtTen.Text.Trim();

                if (_EditState == EditState.CreatingNew)
                {
                    Lists.VatTuHangHoa vthh = DanhMucVatTuHangHoa.Instance.GetByProperty("Ten", tenHang);
                    if (vthh != null)
                    {
                        //MessageBox.Show("Tên thuốc này đã tồn tại trong danh mục với mã: " + vthh.Ma);
                        throw new CustomException("Tên thuốc này đã tồn tại trong danh mục với mã: " + vthh.Ma);
                    }
                    else
                    {
                        vthh = DanhMucVatTuHangHoa.Instance.GetByProperty("Ma", maHang);
                        if (vthh != null)
                        {
                            //MessageBox.Show("Mã thuốc này đã tồn tại trong danh mục với tên: " + vthh.Ten);
                            throw new CustomException("Mã thuốc này đã tồn tại trong danh mục với tên: " + vthh.Ten);
                        }
                    }
                }

                #region Set value
                //buoc phai co
                (_DoiTuong as Lists.VatTuHangHoa).Ma = txtMa.Text.Trim();
                (_DoiTuong as Lists.VatTuHangHoa).Ten = txtTen.Text.Trim();
                (_DoiTuong as Lists.VatTuHangHoa).TrangThai = rdoSuDung.Checked;
                decimal giaBia = CommonLib.ConvertToDecimal(txtGiaBia.EditValue);
                (_DoiTuong as Lists.VatTuHangHoa).GiaBia = giaBia;
                (_DoiTuong as Lists.VatTuHangHoa).NhomVatTuHangHoa = DanhMucNhomVatTuHangHoa.Instance.GetByID((Guid)cmbNhomVatTuHangHoa.EditValue);
                //khong dien cung duoc
                if (txtTenNganGon.Enabled) (_DoiTuong as Lists.VatTuHangHoa).TenNganGon = txtTenNganGon.Text.Trim();
                if (cmbNguonCungCap.Enabled) (_DoiTuong as Lists.VatTuHangHoa).NguonCungCap = cmbNguonCungCap.Text.Trim();
                if (cmbNhaSanXuat.Enabled) (_DoiTuong as Lists.VatTuHangHoa).NhaSanXuat = cmbNhaSanXuat.Text.Trim();
                if (txtChietKhauBanLe.Enabled) (_DoiTuong as Lists.VatTuHangHoa).ChietKhauBanLe = Convert.ToDecimal(txtChietKhauBanLe.EditValue);
                if (txtChietKhauBan.Enabled)
                {
                    decimal ckBan = CommonLib.ConvertToDecimal(txtChietKhauBan.EditValue);
                    (_DoiTuong as Lists.VatTuHangHoa).ChietKhauBan = ckBan;

                    if (DanhMucStore.CurrentStoreNameUpper().Contains("DUOCMINHHANG"))
                    {
                        decimal giaBanBuon = giaBia * (decimal)((100 - ckBan) / 100);
                        (_DoiTuong as Lists.VatTuHangHoa).GiaBanHienTai = giaBanBuon;
                    }
                }
                if (txtChietKhauMua.Enabled) (_DoiTuong as Lists.VatTuHangHoa).ChietKhauMua = Convert.ToDecimal(txtChietKhauMua.EditValue);
                if (cmbDonViTinh.Enabled) (_DoiTuong as Lists.VatTuHangHoa).DonViTinh = cmbDonViTinh.Text.Trim();
                if (txtGhiChu.Enabled) (_DoiTuong as Lists.VatTuHangHoa).GhiChu = txtGhiChu.Text.Trim();
                if (nmrHeSoInMaVach.Enabled) (_DoiTuong as Lists.VatTuHangHoa).HeSoInMaVach = Convert.ToInt32(nmrHeSoInMaVach.Text);
                if (txtHoatChat.Enabled) (_DoiTuong as Lists.VatTuHangHoa).HoatChat = txtHoatChat.Text.Trim();
                if (txtHamLuong.Enabled) (_DoiTuong as Lists.VatTuHangHoa).HamLuong = txtHamLuong.Text.Trim();
                if (txtDuongDung.Enabled) (_DoiTuong as Lists.VatTuHangHoa).DuongDung = txtDuongDung.Text.Trim();
                //(_DoiTuong as Lists.VatTuHangHoa).NhaSanXuat = cmbNhaSanXuat.Text.Trim();
                (_DoiTuong as Lists.VatTuHangHoa).ForOTC = chkForOTC.Checked;
                (_DoiTuong as Lists.VatTuHangHoa).KhongHienKhiKiemTra = chkKhongHienKhiKiemtra.Checked;

                //For NTThanhBinh
                if (DanhMucStore.CurrentStoreNameUpper().Contains("NTTHANHBINH")) //NTThanhBinh yêu cầu hiện trường này
                {
                    if (cmbDangBaoChe.Enabled) (_DoiTuong as Lists.VatTuHangHoa).TacGia = cmbDangBaoChe.Text.Trim();
                    if (txtGiaBanBuon.Visible) (_DoiTuong as Lists.VatTuHangHoa).GiaBanHienTai = Convert.ToDecimal(txtGiaBanBuon.EditValue);
                }

                #endregion
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void rdoSuDung_Click(object sender, EventArgs e)
        {
            rdoKhongSuDung.Checked = false; ;
        }

        private void rdoKhongSuDung_CheckedChanged(object sender, EventArgs e)
        {
            rdoSuDung.Checked = false;
        }

        private void cmbNhomVatTuHangHoa_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbNhomVatTuHangHoa.EditValue is Guid && (Guid)cmbNhomVatTuHangHoa.EditValue != Guid.Empty &&
                    (!Lists.VatTuHangHoa.DaDuocGiaoDich(_DoiTuong.ID) || Config.DuocPhepSuaMaHangKhiDaGiaoDich == DuocPhepSuaMaHangKhiDaGiaoDichHayKhong.DuocPhep))
                {
                    if (chkTenNhomTruocTenhang.Checked)
                    {
                        NhomVatTuHangHoa nhomVTHH = DanhMucNhomVatTuHangHoa.Instance.GetByID((Guid)cmbNhomVatTuHangHoa.EditValue);
                        txtTen.Text = string.Format("[{0}]-", nhomVTHH.Ten);
                    }

                    lblTitle.Text = "Thuốc";
                    lblGiaBia.Text = "Giá bán lẻ";
                    foreach (Control ctrl in this.Controls)
                    {
                        ctrl.Enabled = true;
                    }

                    if (txtMa.Text == "")
                        txtMa.Text = DanhMucVatTuHangHoa.GenBarCode();
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void FrmEditHangHoa_Thuoc_Load(object sender, EventArgs e)
        {
            if (_EditState == EditState.Modifying && Config.DuocPhepSuaDanhMuc == DuocPhepSuaDanhMucHayKhong.KhongDuocPhep)
            {
                cmbNhomVatTuHangHoa.popupList.Properties.ReadOnly = true;
                txtMa.Properties.ReadOnly = true;
                txtTen.Properties.ReadOnly = true;
                txtTenNganGon.Properties.ReadOnly = true;
                txtGiaBia.Properties.ReadOnly = true;
                cmbNhaSanXuat.Enabled = false;
                cmbNguonCungCap.Enabled = false;
                txtChietKhauBanLe.Properties.ReadOnly = true;
                txtChietKhauBan.Properties.ReadOnly = true;
                txtChietKhauMua.Properties.ReadOnly = true;
                cmbDonViTinh.Enabled = false;
                nmrHeSoInMaVach.Properties.ReadOnly = true;
                txtHoatChat.Properties.ReadOnly = true;
                txtGhiChu.Properties.ReadOnly = true;
                txtHamLuong.Enabled = false;
                txtDuongDung.Enabled = false;
            }

            if (!_Store.Ten.ToUpper().Contains("TOTTOT"))
                chkTenNhomTruocTenhang.Checked = false;

            if (_Store.Ten.ToUpper().Contains("NTTHANHBINH"))
            {
                lblGiaBanBuon.Visible = true;
                txtGiaBanBuon.Visible = true;
            }
            else
            {
                lblGiaBanBuon.Visible = false;
                txtGiaBanBuon.Visible = false;
            }
        }
    }
}
